/*
 * Copyright 2018 - 2050 dnkj.All Rights Reserved.
 *
 */
package com.example.webdownloadserver.service;

import com.example.webdownloadserver.constants.TaskCenterConstants;
import com.example.webdownloadserver.dao.TaskCenterDao;
import com.example.webdownloadserver.vo.TaskCenter;
import com.example.webdownloadserver.vo.TaskCenterInput;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.Iterator;
import java.util.List;

/**
 * 下载中心记录表 <br />
 * @taskCreateTime 2020-12-5 12:05:27
 * @author dev-center
 */

@Service
public class TaskCenterServiceImpl implements TaskCenterService {

    @Autowired
    private TaskCenterDao taskCenterDao;

    @Autowired
    private Environment environment;


	@Override
	public Page<TaskCenter> queryTask(TaskCenterInput input) {
		Sort sort = Sort.by(Sort.Direction.DESC,"taskCreateTime");
		TaskCenter taskCenter = new TaskCenter();
		Pageable pageable=  PageRequest.of(input.getPn(), input.getPs(),sort);
		Specification specification = new Specification() {
			@Override
			public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
				Predicate condition1 = null;
				if(StringUtils.isNotBlank(input.getStartTime())) {
					condition1= criteriaBuilder.between(root.get("task_create_time"), input.getStartTime(), input.getEndTime());
				}
				Predicate task_operator = null;
				if(StringUtils.isNotBlank(input.getCreator())){
					task_operator = criteriaBuilder.and(criteriaBuilder.equal(root.get("task_operator"), input.getCreator()));
				}
				Predicate task_business_type = null;
				if(StringUtils.isNotBlank(input.getTaskBusinessType())){
					task_business_type = criteriaBuilder.and(criteriaBuilder.equal(root.get("task_business_type"), input.getTaskBusinessType()));
				}
				criteriaQuery.where(condition1,task_operator,task_business_type);
				return null;
			}
		};
		Page page = taskCenterDao.findAll(specification, pageable);
		return page;
	}

	@Override
	public String dowlnload(Long taskId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<TaskCenter> downloadTaskCenterList(int idle) {
		Sort sort = Sort.by(Sort.Direction.DESC,"taskCreateTime");
		TaskCenter taskCenter = new TaskCenter();
		taskCenter.setTaskType(TaskCenterConstants.TASK_TYPE_DOWNLOAD);
		taskCenter.setTaskStatus(TaskCenterConstants.TASK_STATUS_TODO);
		taskCenter.setTaskSystemSource(TaskCenterConstants.TASK_SYSTEM_SOURCE_DN);
		Pageable pageable=  PageRequest.of(0, idle,sort);
		Page<TaskCenter> all = taskCenterDao.findAll(Example.of(taskCenter), pageable);
		List<TaskCenter> all1 = taskCenterDao.findAll(Example.of(taskCenter));
		System.out.println("list size:"+all1.size());
		Iterator<TaskCenter> iterator = all.iterator();
		while (iterator.hasNext()){
			TaskCenter next = iterator.next();
			next.setTaskStatus(TaskCenterConstants.TASK_STATUS_DOING);
			taskCenterDao.saveAndFlush(next);
		}
		return all.getContent();
	}

	@Override
	public List<TaskCenter> uploadTaskCenterList(int idle) {
		Sort sort = Sort.by(Sort.Direction.DESC,"taskCreateTime");
		TaskCenter taskCenter = new TaskCenter();
		taskCenter.setTaskType(TaskCenterConstants.TASK_TYPE_UPLOAD);
		taskCenter.setTaskStatus(TaskCenterConstants.TASK_STATUS_TODO);
		taskCenter.setTaskSystemSource(TaskCenterConstants.TASK_SYSTEM_SOURCE_DN);
		Pageable pageable=  PageRequest.of(0, idle,sort);
		Page<TaskCenter> all = taskCenterDao.findAll(Example.of(taskCenter), pageable);
		Iterator<TaskCenter> iterator = all.iterator();
		while (iterator.hasNext()){
			TaskCenter next = iterator.next();
			next.setTaskStatus(TaskCenterConstants.TASK_STATUS_DOING);
			taskCenterDao.saveAndFlush(next);
		}
		return all.getContent();
	}

	@Override
	public void update(TaskCenter taskCenter) {
		taskCenterDao.saveAndFlush(taskCenter);
	}

	@Override
	public TaskCenter get(Long taskId) {

		return taskCenterDao.findById(taskId).get();
	}

	@Override
	public void createTask(TaskCenter taskCenter) {
		String[] activeProfiles = environment.getActiveProfiles();
		taskCenter.setProfileActive(activeProfiles[0]);
		taskCenterDao.saveAndFlush(taskCenter);
	}
}
